import React, { Component } from 'react';
import { connect } from 'dva';
import { Button, Icon, Popconfirm } from 'antd';

import "./DraftHistory.scss";

@connect(({ add }) => ({
    add: add
}))
class DraftHistory extends Component {
    constructor(props) {
        super(props);
        this.state = {
            current_draftId: null
        };
    }
    

    openDraft = (id, type) => {
        console.log(id, type);
        this.setState({
            current_draftId: id
        })
        this.props.dispatch({
            type: 'add/getCurrentDraft', 
            payload: { 
                id: id
            },
            callback: (data) => {
                this.props.draftChange(data)
            }
        })
    }
   
    delteDraft = (id) => {
        this.props.dispatch({
            type: "add/deleteDraft",
            payload: {
                id: id
            }
        })
    }

    render() {
        const history_list = this.props.add.infoHistoryList || [];
        const current_draftId = this.state.current_draftId || (history_list.length > 0 ? history_list[history_list.length-1].id : null);
        return (
            <div className="container">
                {
                    history_list.length > 0 ?
                    history_list.map((draft, i) => {
                        return <div className="draft" key={draft.id.toString()}>
                            <span className={current_draftId == draft.id ? "dot dot_active" : "dot" }></span>
                            {/* <div className="draft_info" onClick={() => this.openDraft(draft.id, 0)}> */}
                            <div className="draft_info">
                                <Popconfirm
                                    title="是否切换到历史版本?"
                                    onConfirm={() => this.openDraft(draft.id, 0)}
                                    onCancel={() => console.log("取消了")}
                                    okText="确定"
                                    okType=""
                                    cancelText="取消"
                                >
                                    <a href="javascript:void(0);">{draft.edit_time}</a>
                                    <br/>
                                    <span>{draft.user_name}</span>
                                </Popconfirm>
                            </div>
                            <div>
                                {/* <a href="javascript:void(0);"><Icon type="edit" className="ml10" onClick={() => this.openDraft(draft.id, 1)} /></a> */}
                                <a href="javascript:void(0);"><Icon type="delete" className="ml10" onClick={() => this.delteDraft(draft.id)} /></a>
                            </div>
                        </div>
                    })
                    :
                    "暂无稿件历史版本"
                }
            </div>
        );
    }
}

export default DraftHistory;